package com.ahcloud.edas.gateway.core.application.helper;

import com.ahcloud.edas.common.domain.common.PageResult;
import com.ahcloud.edas.gateway.core.domain.plugin.form.PluginAddForm;
import com.ahcloud.edas.gateway.core.domain.plugin.form.PluginUpdateForm;
import com.ahcloud.edas.gateway.core.domain.plugin.vo.PluginDetailVO;
import com.ahcloud.edas.gateway.core.domain.plugin.vo.PluginVO;
import com.ahcloud.edas.gateway.core.infrastructure.repository.bean.GatewayPlugin;
import com.ahcloud.edas.uaa.client.UserUtils;
import com.github.pagehelper.PageInfo;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: ahcloud-gateway
 * @description:
 * @author: YuKai Fan
 * @create: 2023/2/4 11:03
 **/
public class GatewayPluginHelper {

    /**
     * 数据转换
     * @param form
     * @return
     */
    public static GatewayPlugin convert(PluginAddForm form) {
        GatewayPlugin GatewayPlugin = Convert.INSTANCE.convert(form);
        String userNameBySession = UserUtils.getUserNameBySession();
        GatewayPlugin.setCreator(userNameBySession);
        GatewayPlugin.setModifier(userNameBySession);
        return GatewayPlugin;
    }

    /**
     * 数据转换
     * @param form
     * @return
     */
    public static GatewayPlugin convert(PluginUpdateForm form) {
        GatewayPlugin gatewayPlugin = Convert.INSTANCE.convert(form);
        gatewayPlugin.setModifier(UserUtils.getUserNameBySession());
        return gatewayPlugin;
    }

    /**
     * 数据转换
     * @param GatewayPlugin
     * @return
     */
    public static PluginDetailVO convertToVo(GatewayPlugin GatewayPlugin) {
        return Convert.INSTANCE.convertToDetailVo(GatewayPlugin);
    }

    /**
     * 数据转换
     * @param pageInfo
     * @return
     */
    public static PageResult<PluginVO> convertToPageResult(PageInfo<GatewayPlugin> pageInfo) {
        List<GatewayPlugin> list = pageInfo.getList();
        List<PluginVO> PluginVOList = Convert.INSTANCE.convertToVoList(list);
        PageResult<PluginVO> pageResult = new PageResult<>();
        pageResult.setPageSize(pageInfo.getPageSize());
        pageResult.setPageNum(pageInfo.getPageNum());
        pageResult.setPages(pageInfo.getPages());
        pageResult.setRows(PluginVOList);
        pageResult.setTotal(pageInfo.getTotal());
        return pageResult;
    }

    @Mapper
    public interface Convert {
        Convert INSTANCE = Mappers.getMapper(Convert.class);

        /**
         * 数据转换
         * @param form
         * @return
         */
        GatewayPlugin convert(PluginAddForm form);

        /**
         * 数据转换
         * @param form
         * @return
         */
        GatewayPlugin convert(PluginUpdateForm form);

        /**
         * 数据转换
         * @param GatewayPlugin
         * @return
         */
        PluginDetailVO convertToDetailVo(GatewayPlugin GatewayPlugin);

        /**
         * 数据转换
         * @param GatewayPlugin
         * @return
         */
        PluginVO convertToVo(GatewayPlugin GatewayPlugin);

        /**
         * 数据转换
         * @param GatewayPluginList
         * @return
         */
        List<PluginVO> convertToVoList(List<GatewayPlugin> GatewayPluginList);
    }
}
